# frozen_string_literal: true

module ActiveRecord
  module ConnectionAdapters
    module SQLServer
      class SchemaDumper < ConnectionAdapters::SchemaDumper
        SQLSERVER_NO_LIMIT_TYPES = %w[text ntext varchar(max) nvarchar(max) varbinary(max)].freeze

        private

        def prepare_column_options(column)
          spec = super

          if @connection.supports_virtual_columns? && column.virtual?
            spec[:as] = extract_expression_for_virtual_column(column)
            spec[:stored] = column.virtual_stored?
          end

          spec
        end

        def extract_expression_for_virtual_column(column)
          column.default_function.inspect
        end

        def explicit_primary_key_default?(column)
          column.type == :integer && !column.is_identity?
        end

        def schema_limit(column)
          return if SQLSERVER_NO_LIMIT_TYPES.include?(column.sql_type)

          super
        end

        def schema_collation(column)
          return unless column.collation

          # use inspect to ensure collation is dumped as string. Without this it's dumped as
          # a constant ('collation: SQL_Latin1_General_CP1_CI_AS')
          collation = column.collation.inspect
          # use inspect to ensure string comparison
          default_collation = @connection.collation.inspect

          collation if collation != default_collation
        end

        def default_primary_key?(column)
          super && column.is_identity?
        end

        def schemas(stream)
          schema_names = @connection.schema_names

          if schema_names.any?
            schema_names.sort.each do |name|
              stream.puts "  create_schema #{name.inspect}"
            end
            stream.puts
          end
        end
      end
    end
  end
end
